//
//  NetworkTools.m
//  我的微博
//
//  Created by tom-1 on 15/12/8.
//  Copyright © 2015年 tom-1. All rights reserved.
//

#import "NetworkTools.h"
// 网络工具协议
@protocol NetworkToolsProxy <NSObject>

// 网络请求方法
@optional
- (NSURLSessionDataTask *)dataTaskWithHTTPMethod:(NSString *)method
                                       URLString:(NSString *)URLString
                                      parameters:(id)parameters
                                         success:(void (^)(NSURLSessionDataTask *, id))success
                                         failure:(void (^)(NSURLSessionDataTask *, NSError *))failure;

@end

// 遵守网络协议 - 为了能够欺骗 Xcode 给一个智能提示，以及编译通过！
@interface NetworkTools() <NetworkToolsProxy>

@end


@implementation NetworkTools
// 应用程序信息

static NSString *appKey = @"3863118655";
static NSString *appSecret = @"b94c088ad2cdae8c3b9641852359d28c";
static NSString *redirectURL = @"http://www.baidu.com";

+ (instancetype)sharedTools {
    static NetworkTools *tools;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSURL *baseURL = [NSURL URLWithString:@"http://httpbin.org/"];
        tools = [[self alloc] initWithBaseURL:baseURL];
        NSString *str = [NSString stringWithFormat:@"https://api.weibo.com/oauth2/authorize?client_id=%@&redirect_uri=%@",appKey,redirectURL];
        tools.oauthURL = [NSURL URLWithString:str];
        
        // 设置反序列化格式
        tools.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/plain", nil];
        
    });
    return tools;
}
#pragma mark - 用户相关方法
- (void)loadUserInfo:(NSString *)uid accessToken:(NSString *)accessToken finished:(void (^)(id, NSError *))finished {
    NSString *urlString = @"https://api.weibo.com/2/users/show.json";
    NSDictionary *params = @{@"uid":uid,
                             @"access_token":accessToken};
    [self request:GET URLString:urlString parameters:params finished:finished];
}


#pragma mark - OAuth相关方法

- (void)loadAccessToken:(NSString *)code finished:(void (^)(id, NSError *))finished {
    
    
    // 加载 AccessToken
    NSString *urlString = @"https://api.weibo.com/oauth2/access_token";
    NSDictionary *params = @{@"client_id": appKey,
        @"client_secret": appSecret,
        @"grant_type": @"authorization_code",
        @"code": code,
        @"redirect_uri": redirectURL};
    
    [self request:POST URLString:urlString parameters:params finished:finished];
    
}


#pragma mark - 封装AFN网络方法

- (void)request: (TomRequestMethod)method URLString: (NSString *)URLString parameters: (id)parameters finished: (void (^)(id result, NSError *error))finished {
    NSString *methodName = (method == GET) ? @"GET" : @"POST";
    
    [[self dataTaskWithHTTPMethod:methodName URLString:URLString parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
        
        finished(responseObject, nil);
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"%@", error);
        finished(nil, error);
    }] resume];
}
@end
